package com.pan.admin.common.constant.module;

import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.module.channel.entity.qo.LoanChannelStatisticQO;
import com.pan.admin.module.distribute.entity.qo.LoanDistributeStatisticQO;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 视图枚举
 */
public enum StatisticEnum {

    HOURLY("0", "每小时", "%H") {
        @Override
        public LoanDistributeStatisticQO getDistributeStatisticQO(Long groupId) {
            return new LoanDistributeStatisticQO(groupId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        public LoanChannelStatisticQO getChannelStatisticQO(Long channelId) {
            return new LoanChannelStatisticQO(channelId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        String getStartTime() {
            return DateUtils.getDate() + " 00:00:00";
        }

        @Override
        String getEndTime() {
            return DateUtils.getDate() + " 23:59:59";
        }
    },
    DAILY("1", "每天", "%Y-%m-%d") {
        @Override
        public LoanDistributeStatisticQO getDistributeStatisticQO(Long groupId) {
            return new LoanDistributeStatisticQO(groupId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        public LoanChannelStatisticQO getChannelStatisticQO(Long channelId) {
            return new LoanChannelStatisticQO(channelId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        String getStartTime() {
            LocalDate now = LocalDate.now();
            return LocalDate.of(now.getYear(), now.getMonth(), 1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00:00:00";
        }

        @Override
        String getEndTime() {
            LocalDate now = LocalDate.now();
            return LocalDate.of(now.getYear(), now.getMonth(), now.getDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 23:59:59";
        }
    },
    MONTHLY("2", "每月", "%Y-%m") {
        @Override
        public LoanDistributeStatisticQO getDistributeStatisticQO(Long groupId) {
            return new LoanDistributeStatisticQO(groupId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        public LoanChannelStatisticQO getChannelStatisticQO(Long channelId) {
            return new LoanChannelStatisticQO(channelId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        String getStartTime() {
            LocalDate now = LocalDate.now();
            return LocalDate.of(now.getYear(), 1, 1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }

        @Override
        String getEndTime() {
            LocalDate now = LocalDate.now();
            LocalDate endTimeDate = LocalDate.of(now.getYear(), 12, 1);
            return endTimeDate.withDayOfMonth(endTimeDate.lengthOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
    },
    NOW("3", "当天", "%Y-%m-%d") {
        @Override
        public LoanDistributeStatisticQO getDistributeStatisticQO(Long groupId) {
            return new LoanDistributeStatisticQO(groupId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        public LoanChannelStatisticQO getChannelStatisticQO(Long channelId) {
            return new LoanChannelStatisticQO(channelId, getFormat(), getStartTime(), getEndTime());
        }

        @Override
        String getStartTime() {
            return DateUtils.getDate() + " 00:00:00";
        }

        @Override
        String getEndTime() {
            return DateUtils.getDate() + " 23:59:59";
        }
    };

    private static final Map<String, StatisticEnum> MAP = initMap();
    private final String type;
    private final String typeName;
    private final String format;

    /**
     * 获取传递的SQL的开始时间以及结束时间
     *
     * @return String
     */
    public abstract LoanDistributeStatisticQO getDistributeStatisticQO(Long groupId);

    public abstract LoanChannelStatisticQO getChannelStatisticQO(Long channelId);

    abstract String getStartTime();

    abstract String getEndTime();

    /**
     * 构造函数
     *
     * @param type      统计类型
     * @param typeName  类型名称
     * @param format    格式化时间
     */
    private StatisticEnum(String type, String typeName, String format) {
        this.type = type;
        this.typeName = typeName;
        this.format = format;
    }

    public String getType() {
        return type;
    }

    public String getTypeName() {
        return typeName;
    }

    public String getFormat() {
        return format;
    }

    /**
     * 获取枚举
     *
     * @param viewType
     * @return
     */
    public static StatisticEnum get(String viewType) {
        StatisticEnum statisticEnum = MAP.get(viewType);
        if (statisticEnum == null) {
            throw new RuntimeException("错误的viewType");
        }
        return statisticEnum;
    }

    private static Map<String, StatisticEnum> initMap() {
        Map<String, StatisticEnum> map = new HashMap<>();
        map.put("0", StatisticEnum.HOURLY);
        map.put("1", StatisticEnum.DAILY);
        map.put("2", StatisticEnum.MONTHLY);
        map.put("3", StatisticEnum.NOW);
        return map;
    }
}
